package com.qst.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.*;
import com.qst.query.BaseQuery;
import com.qst.query.UserQuery;
import com.qst.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("toLogin")
    public String toLogin() {

        return "login";

    }



    //    框架加载就会到controller找对应的地址
    @RequestMapping("toTop01")
    public String toTop() {
        return "tip01";
    }

    @RequestMapping("toLeft01")
    public String ToLeft() {
        return "left01";
    }

    @RequestMapping("toMain01")
    public String ToMain() {
        return "main01";
    }

    @RequestMapping("toTop02")
    public String toTop2() {
        return "tip02";
    }

    @RequestMapping("toLeft02")
    public String ToLeft2() {
        return "left02";
    }

    @RequestMapping("toMain02")
    public String ToMain2() {
        return "main02";
    }


    @RequestMapping("login")
    public String login(String userName, String userPwd, Integer roleId,String verityCode, HttpSession session, Model model) {

        Result result = userService.login(userName, userPwd, roleId, verityCode, session);

        if (result.getCode() == 200) {
            if(roleId==1){
                //登录成功
                User userInfo = (User) result.getData();
//为了让拦截器判断
                session.setAttribute("userInfo", userInfo);
                return "index01";
            }
           else{
               //登录成功
               User userInfo = (User) result.getData();
//为了让拦截器判断
               session.setAttribute("userInfo", userInfo);
               return "index02";
           }

        } else {

            //登录失败
            model.addAttribute("msg", result.getMsg());

            return "login";

        }

    }

    @RequestMapping("register")
    public String register(User user,String verityCode, HttpSession session, Model model) {

        Result result = userService.register(user,verityCode, session);

        if (result.getCode() == 200) {

            //注册成功
//            User userInfo = (User) result.getData();
//
//            session.setAttribute("userInfo",userInfo);

            model.addAttribute("msg", result.getMsg());

            return "toRegister";

        } else {

            //登录失败
            model.addAttribute("msg", result.getMsg());

            return "toRegister";

        }
    }

    //  退出
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
//        清除session
        session.invalidate();
        return "login";
    }

       // 携带用户数据跳转到用户显示界面
    //@RequestMapping("TouserInfo")

//    首次没有传userName就是为空,查询所有
    //public String ToUserInfo(UserQuery userQuery, Model model) {
//        查询所有用户，将信息带入userInfo
//        启动分页插件
      //  PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());

      //  List<User> userList = userService.getUserList(userQuery);
//        首页，上一页，下一页，尾页 至少需要当前页，总页数，总记录数，需要一个类进行封装，插件提供了pageinfo类
      //  PageInfo<User> pageInfo = new PageInfo<>(userList);
//        userList被封装到pageInfo
     //   model.addAttribute("userList", pageInfo);
       // model.addAttribute("userName", userQuery.getUserName());
      //  return "userInfo";
    //}

    @RequestMapping("ToUserInfo")
//    首次没有传userName就是为空,查询所有
    public String ToUserInfo(Model model,HttpSession session) {
       User user = (User) session.getAttribute("userInfo");
       model.addAttribute("user",user);
        return "userInfo";
    }

    //表单提交userName,搜索用户
//    @RequestMapping("getUserListByParam")
//    public String getUserListByParam(String userName, Model model, BaseQuery baseQuery) {
//        PageHelper.startPage(baseQuery.getPageNum(), baseQuery.getPageSize());
////        模糊查询
//        List<User> userList = userService.getUserListByParam(userName);
//        PageInfo<User> pageInfo = new PageInfo<>(userList);
////        回显要修改的用户名
//        model.addAttribute("userName", userName);
////        返回搜索后的用户
//        model.addAttribute("userList", pageInfo);
//        return "userInfo";
//    }

    //转到添加页面
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "test";
    }
//    表单提交，获取数据进行添加，回显msg



//    @RequestMapping("/addUser")
//    public String addUser(String userName, String userPwd, Integer gender, Model model,Integer roleId) {
//        Result result = userService.register(userName, userPwd, gender,roleId);
//        if (result.getCode() == 200) {
////刷新
////调用     ToUserInfo方法将数据带过去
////            或者再查询
//            UserQuery userQuery = new UserQuery();
//            return ToUserInfo(userQuery, model);
//
//        } else {
////            添加失败，返回失败信息
//            model.addAttribute("msg", result.getMsg());
//            return "toAdd";
//        }
//    }

    //根据id查找user，携带user跳转编辑界面
    @RequestMapping("/toEdit")
    public String toEdit(Integer id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "userUpdate";
    }

    //表单提交，自动会封装成对象来接收那三个参数，进行编辑，回显msg
    @RequestMapping("editUser")
//    @PostMapping("editUser")
//    @ResponseBody
    public String editUser( User user, Model model) {
        System.out.println(user.getUserPwd());
        Result result = userService.editUser(user);
        model.addAttribute("msg", result.getMsg());

        if (result.getCode() == 200) {
            //添加成功
        User user1=  userService.getById(user.getId());
        model.addAttribute("user",user1);
        return "userInfo";

        } else {
            //添加失败
            model.addAttribute("msg", result.getMsg());
            return "userUpdate";
        }

    }

    //根据id删除，回显msg信息
    @RequestMapping("/deleteUser")
    public String deleteUser(Integer id, Model model) {
        Result result = userService.deleteUser(id);
        if (result.getCode() == 200) {
//            UserQuery userQuery = new UserQuery();
//            return ToUserInfo(userQuery, model);
        }
//        失败啥也不显示
        return null;
    }

    //跳转注册页面
    @RequestMapping("/toRegister")
    public String toRegister() {
        return "toRegister";
    }

    @RequestMapping("/delBatch")
    public String delBatch(Integer[] id, Model model) {

        Result result = userService.delBatch(id);

//        model.addAttribute(result);

        if (result.getCode() == 200) {

            UserQuery userQuery = new UserQuery();
//            model.addAttribute("msg", result.getMsg());
//            return ToUserInfo(userQuery, model);
        }
        return null;
    }

@RequestMapping("/toMainPage")
public String toMainPage(){
        return "index02";
}
    // 携带用户数据跳转到用户显示界面
    //@RequestMapping("TouserInfo")

//    首次没有传userName就是为空,查询所有
    //public String ToUserInfo(UserQuery userQuery, Model model) {
//        查询所有用户，将信息带入userInfo
//        启动分页插件
    //  PageHelper.startPage(userQuery.getPageNum(), userQuery.getPageSize());

    //  List<User> userList = userService.getUserList(userQuery);
//        首页，上一页，下一页，尾页 至少需要当前页，总页数，总记录数，需要一个类进行封装，插件提供了pageinfo类
    //  PageInfo<User> pageInfo = new PageInfo<>(userList);
//        userList被封装到pageInfo
    //   model.addAttribute("userList", pageInfo);
    // model.addAttribute("userName", userQuery.getUserName());
    //  return "userInfo";
    //}

    @RequestMapping("/getMenuList01")
    @ResponseBody
    public List<Menu> getMenuList01(){
        return userService.getMenuList01();
    }

    @RequestMapping("/getMenuList02")
    @ResponseBody
    public List<Menu> getMenuList02(){
        return userService.getMenuList02();
    }

    @RequestMapping("/getRoleList")
    @ResponseBody
    public List<Role> getRoleList(){
        return userService.gerRoleList();
    }

    @RequestMapping("/getEchartsData")
    @ResponseBody
    public List<EchartsData> getEchartsData(){
        return userService.getEchartsData();
    }

    @RequestMapping("/getEchartsDataName")
    @ResponseBody
    public List<String> getEchartsDataName(){
        return userService.getEchartsDataName();
    }

    @RequestMapping("/getEchartsDataValue")
    @ResponseBody
    public List<String> getEchartsDataValue(){
        return userService.getEchartsDataValue();
    }

    @RequestMapping("/toEcharts")
    public String toEcharts(){
        return "echarts02";
    }

    @RequestMapping("/toEcharts02")
    public String toEcharts02(){
        return "echarts";
    }

}
